package cn.edu.sdjzu.xg23.kcsj01_backend.controller;

import cn.edu.sdjzu.xg23.kcsj01_backend.entity.ApprovalState;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Instruction;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.impl.InstructionServiceImpl;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/instruction")
public class InstructionController {

    @Autowired
    private InstructionServiceImpl instructionServiceImpl;

    @PreAuthorize("hasAuthority('instruction:add')")
    @PostMapping
    public Result addInstruction(@RequestBody Instruction instruction) {
        if (instructionServiceImpl.addInstruction(instruction)) {
            return Result.success("保存成功");
        }
        return Result.error("013", "保存失败");
    }

    @PreAuthorize("hasAuthority('instruction:update')")
    @PutMapping
    public Result update(@RequestBody Instruction instruction) {
        if (instructionServiceImpl.updateInstruction(instruction)) {
            return Result.success("更新成功");
        }
        return Result.error("015", "更新失败");
    }

    @PreAuthorize("hasAuthority('instruction:delete')")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        if (instructionServiceImpl.deleteInstruction(id)) {
            return Result.success("删除成功");
        }
        return Result.error("014", "删除失败");
    }

    @PreAuthorize("hasAuthority('instruction:search:all')or hasAuthority('instruction:search:my')")
    @GetMapping
    public Result selectInstructionsByCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,             // 拆解为基本类型参数
            @RequestParam(required = false) Long employeeId,
            @RequestParam(required = false) String employeeName,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date applicationTime,
            @RequestParam(required = false) Long approvalState) {

        Instruction instruction = new Instruction();
        instruction.setName(name);

        // 将日期参数设置到instruction对象中
        if (applicationTime != null) {
            instruction.setDate(applicationTime);
        }

        // 将审批状态参数设置到instruction对象中
        if (approvalState != null) {
            ApprovalState state = new ApprovalState();
            state.setId(approvalState);
            instruction.setApprovalState(state);
        }

        PageInfo<Instruction> pageInfo = instructionServiceImpl.selectInstructionsByCondition(
                pageNum, pageSize, instruction, employeeId, employeeName);
        return Result.success(pageInfo);
    }
    @PreAuthorize("hasAuthority('instruction:approve')")
    @PutMapping("/approve")
    public Result approveInstruction(@RequestBody Instruction instruction) {
        if (instructionServiceImpl.approveInstruction(instruction)) {
            return Result.success("审批状态更新成功");
        }else {
            return Result.error("016", "审批状态更新失败");
        }
    }
}